package com.cjxt.ams.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjxt.ams.dao.DeptDao;
import com.cjxt.ams.dao.RoleDao;
import com.cjxt.ams.dao.UserDao;
import com.cjxt.ams.domain.Dept;
import com.cjxt.ams.domain.Role;
import com.cjxt.ams.domain.User;
import com.cjxt.ams.domain.UserRole;
import com.cjxt.ams.service.UserRoleService;
import com.cjxt.ams.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * @author zhanghuan
 * @version V1.0
 * @Description:
 * @className: UserServiceImpl
 * @date 2021/11/23 10:11
 * @company:华勤技术股份有限公司
 * @copyright: Copyright (c) 2021
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    @Resource
    private UserDao userDao;

    @Resource
    private DeptDao deptDao;

    @Resource
    private RoleDao roleDao;

    @Resource
    UserService userService;

    @Resource
    UserRoleService userRoleService;

    //根据用户ID查询用户的所有菜单ID
    @Override
    public List<Integer> queryAllMenuId(Integer userId) {
        return baseMapper.queryAllMenuId(userId);
    }

    @Override
    public String getSUUID() {
        String s = UUID.randomUUID().toString();
        //去掉“-”符号
        return s.substring(0,8);
    }

    @Override
    public boolean judgeUserAccount(String userAccount) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("useraccount",userAccount);
        List<User> list=this.getBaseMapper().selectList(wrapper);
        if (list != null && list.size() > 0) {
            User temp = list.get(0);
            BeanUtils.copyProperties(temp, userAccount);
            return true;
        }
        return false;
    }

    @Override
    public void del(Integer userId) {
        userService.removeById(userId);

        QueryWrapper<UserRole> qw = new QueryWrapper<>();
        qw.eq("user_id", userId);

        userRoleService.remove(qw);
    }

    @Override
    public IPage<User> selectUser(User user) {
        IPage<User> userIPage = new Page<>(user.getPage(),user.getLimit());
        QueryWrapper queryWrapper = new QueryWrapper();
        if(StrUtil.isNotEmpty(user.getUsername())){
            queryWrapper.like("username",user.getUsername());
        }
        queryWrapper.orderBy(true,false,"id");
        userIPage = userDao.selectPage(userIPage,queryWrapper);
        List<User> list = userIPage.getRecords();
        for (int i = 0 ; i < list.size() ; i++){
            User user1 = list.get(i);
            Dept dept = deptDao.selectById(user1.getDeptId());
            if(dept != null){
                String deptName = dept.getName();
                user1.setName(deptName);
            }
            List<Role> roleList = roleDao.getRolesByUserId(user1.getUserId());
            String roleNames = "";
            for (int j = 0 ; j<roleList.size() ; j++){
                Role role = roleList.get(j);
                String str = role.getRoleName();
                if (j>0) {
                    roleNames += ","+str;
                }else {
                    roleNames = str;
                }
            }
            user1.setRoleName(roleNames);

        }
        return userIPage;
    }

}
